<?php
/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: dzf_base_control.php 4160 2014-09-10 04:12:38Z laoguozhang $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class dzf_base_control extends dzf_base_model
{
	protected $_title = '';
	protected $_view = null;
	public $actions = array();
	public $default_action='index'; //默认action
	public $action = '';//当前的action

	protected $_code = 0;
	protected $_msg = '';
	public $data = array();

	public function __construct() {
		parent::__construct();
	}

	public function init() {

	}

	public function __call($name, $parameters) {
		try {
			$this->run($this->default_action);
		} catch (Exception $e) {
			parent::__call($name, $parameters);
		}

	}

	public function run($action) {
		if(isset($this->actions[$action])) {
			try {
				$cname = $this->actions[$action]['class'];
				$obj = new $cname();
				$obj->run();
			} catch (Exception $e) {
				//run default action
				throw new Exception('');
			}
		} else {
			$this->action = $action;
			$action = 'action_'.$this->action;
			$this->$action();
		}
	}

	public function render($view, $data = array(), $return = false) {
		if($this->_view === null) {
			$this->init_view($this);
		}
		return $this->_view->render($view, $data, $return);
	}
	
	public function assign($name, $value = null) {
		$this->_view->assign($name, $value);
	}

	public function init_view() {
		if ($this->_view === null) {
			$this->_view = DZF::app()->init_view();
			if(empty($this->_view)) {
				return false;
			}
			$this->view_path('default');
		}
		return $this;
	}

	public function view_path($path = NULL, $isrealpath = false) {
		if(isset($path)) {
			$this->_view->path($isrealpath ? $path : APP_PATH.'/'.DZF::app()->appname.'/view/'.$path);
		} else {
			return $this->_view->path();
		}
		return $this;
	}
	
	
	public function view_ext($name = NULL) {
		if(isset($name)) {
			$this->_view->ext_name = $name;
		} else {
			return $this->_view->ext_name;
		}
		return $this;
	}
	
	public function redirect() {

	}

	public function forward($route,$exit=true) {
		if(isset($route['controller'])) {
			DZF::app()->run_controller($route);
		}
		if($exit) {
			DZF::app()->end();
		}
	}

	public function title($title = NULL) {
		if(isset($title)) {
			$this->_title = $title;
		} else {
			return $this->_title;
		}
		return $this;
	}

	protected function _init_parameters($parameters){}


	/**
	 * 
	 * @return dzf_base_request
	 */
	public function request() {
		return $this->app->request();
	}
	
	/**
	 * 设置跨域请求header
	 * @param type $request_method
	 * @param type $origin
	 */
	function make_cors($request_method, $origin = '*') {

		if ($request_method === 'OPTIONS') {
			// 这个*可以设置为想允许的域名比如
			header('Access-Control-Allow-Origin:'.$origin);

			/**
			* 是否允许发送cookie，以及支持的请求。
			*/
			header('Access-Control-Allow-Credentials:true');
			header('Access-Control-Allow-Methods:GET, POST, OPTIONS');

			// 自定义一些头，这个也可以当作一个密钥，必须与请求时候的头是一致的。
			//header('Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');

			/**
			// 设置一个过期时间，由于options只是一个握手的工作，所以时间可以设的长一点儿
			 *
			 */
			header('Access-Control-Max-Age:1728000');
			header('Content-Type:text/plain charset=UTF-8');
			header("status: 204");
			header('HTTP/1.0 204 No Content');
			header('Content-Length: 0',true);
			//header('Content-Type: text/html',true);
			flush();
		}

		// 真实的请求数据
		if ($request_method === 'POST') {

			header('Access-Control-Allow-Origin:'.$origin);
			header('Access-Control-Allow-Credentials:true');
			header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
			//header('Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');
		}

		if ($request_method === 'GET') {

			header('Access-Control-Allow-Origin:'.$origin);
			header('Access-Control-Allow-Credentials:true');
			header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
			//header('Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');
		}

		//credentials 使用注意 http://msdn.microsoft.com/zh-cn/library/ie/dn423949(v=vs.85).aspx
		//SEC7121 "当凭据标志设置为 True 时，不允许 Access-Control-Allow-Origin 中的通配符。
		//服务器正在标头中返回“Access-Control-Allow-Origin: *”，但当在 XMLHttpRequest 中将 withCredentials 标志设置为 True 时，则不允许该操作。
		//需要修改服务器端处理程序以返回“Access-Control-Allow-Origin”标头，该标头特别允许此类请求上的原点。如果你不能控制服务器端处理程序，则需要与执行此操作的开发人员联系。
		//
		//client:xhr.withCredentials = true;
		//server:header('Access-Control-Allow-Credentials:true');
	}
}